home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 16 / polygons.fth < prev    next >
Text File  |  1985-11-19  |  3KB  |  88 lines

  1. \ Sliding polygons demo.  Load this file and type   poly-demo
  2. \ Type any key to stop it.
  3. \
  4. \ The demo initially draws triangles.  By typing
  5. \    n vertices   poly-demo
  6. \ where 3 <= n < 64, you can change it to draw polygons with more vertices
  7.  
  8. needs line-a-init linea.fth
  9. line-a-init
  10. decimal
  11. 17 xbios: _random { -- l.rnd-number }  \ bios random number routine
  12.  5 xbios: _setscreen  { w.rez l.phyz l.log -- }
  13. 37 xbios: vsync { -- }  \ wait till next vertical retrace then return
  14.  
  15. : rnd  ( limit -- rndnum )  \ return random number up to limit
  16.    _random swap  mod
  17. ;
  18.  
  19. scradr constant old-screen
  20. get-rez
  21. constant cury
  22. constant curx
  23.  
  24. create source-buff  256 allot
  25. create dest-buff  256 allot
  26. create spare-buff  256 allot
  27. create new-scr-buff  1024 32 * 512 + allot
  28. new-scr-buff 512 mod 512 swap - new-scr-buff + constant new-screen
  29. variable keep-going keep-going on
  30. variable #verts  3 #verts !
  31.  
  32. : set-buf  ( addr -- )  \ randomize a buffer
  33.    dup #verts @ 0
  34.    do   curx rnd  over w! 2+  cury rnd  over w! 2+   loop
  35.    over w@ over w! 2+ swap 2+ w@  swap w!
  36. ;
  37.  
  38. : move-buf  ( -- )  \ convert one buffer to another
  39.    dest-buff  source-buff  keep-going off  #verts @ 1+  2* 0
  40.    do
  41.      dup w@  2 pick w@  2dup 2dup <> -rot 1+ <> and
  42.       if   over  > 
  43.         if   2+
  44.         else 2-
  45.         then over w!  keep-going on
  46.       else 2drop
  47.       then 2+  swap  2+  swap
  48.    loop  2drop
  49. ;
  50. : draw-source ( -- )  \ draw the source polygon
  51.    source-buff #verts @  poly-line 
  52. ;
  53. : vertices  ( n -- )  \ set the number of vertices
  54.    #verts !
  55. ;
  56. : poly-demo
  57.    27 emit ascii f emit
  58.    -1 -1 new-screen _setscreen  erase-screen
  59.    -1 -1 old-screen _setscreen  erase-screen
  60.    2 _wrt_mod w!  
  61.    dest-buff set-buf  source-buff set-buf  
  62.    -1 new-screen old-screen _setscreen vsync draw-source  
  63.    -1 old-screen new-screen _setscreen vsync draw-source  
  64.    source-buff spare-buff  #verts @ 1+ 4 * cmove
  65.    begin
  66.     begin
  67.         -1 old-screen new-screen  _setscreen
  68.         spare-buff #verts @ vsync poly-line        
  69.         source-buff spare-buff  #verts @ 1+ 4 * cmove
  70.         move-buf  draw-source 
  71.  
  72.         -1 new-screen old-screen  _setscreen
  73.         spare-buff #verts @ vsync poly-line        
  74.         source-buff spare-buff  #verts @ 1+ 4 * cmove
  75.         move-buf  draw-source 
  76.         key?
  77.         if  -1 old-screen old-screen _setscreen
  78.             27 emit ascii e emit exit  
  79.         then
  80.           keep-going @ 0=
  81.     until  dest-buff set-buf 
  82.    again
  83. ;
  84.  -) move
  85. c;
  86.  
  87. : ne